/* GUI CONVERTED from rwroru.p (converter v1.76) Sun Aug 25 21:00:08 2002 */
/* rwroru.p - ROUTING ROLL-UP                                           */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                  */
/* All rights reserved worldwide.  This is an unpublished work.         */
/* $Revision: 1.11.1.13 $                                                         */
/*V8:ConvertMode=ReportAndMaintenance                                   */
/* REVISION: 1.0        LAST MODIFIED: 09/22/86     BY: EMB             */
/* REVISION: 2.0        LAST MODIFIED: 04/09/87     BY: EMB *A47*       */
/* REVISION: 4.0        LAST MODIFIED: 02/17/88     BY: RL  *A171*      */
/* REVISION: 4.0        LAST MODIFIED: 04/11/88     BY: FLM *A196*      */
/* REVISION: 4.0        LAST MODIFIED: 04/27/88     BY: EMB *A205*      */
/* REVISION: 4.0        LAST MODIFIED: 01/10/89     BY: FLM *A580*      */
/* REVISION: 4.0        LAST MODIFIED: 02/02/89     BY: RL  *A634*      */
/* REVISION: 5.0        LAST MODIFIED: 07/05/89     BY: BJJ *B106*      */
/* REVISION: 5.0        LAST MODIFIED: 07/21/89     BY: EMB *B169*      */
/* REVISION: 5.0        LAST MODIFIED: 08/23/90     BY: emb *B775*      */
/* REVISION: 5.0        LAST MODIFIED: 12/06/90     BY: emb *B842*      */
/* REVISION: 6.0        LAST MODIFIED: 06/24/90     BY: emb *D001*      */
/* REVISION: 6.0        LAST MODIFIED: 05/11/90     BY: RAM *D018*      */
/* REVISION: 6.0        LAST MODIFIED: 08/02/91     BY: emb *D807*      */
/* REVISION: 7.0        LAST MODIFIED: 12/06/91     BY: pma *F003*      */
/* REVISION: 7.0        LAST MODIFIED: 12/09/91     BY: pma *F069*      */
/* REVISION: 7.0        LAST MODIFIED: 04/02/92     BY: emb *F347*      */
/* REVISION: 7.0        LAST MODIFIED: 05/28/92     BY: pma *F542*      */
/* REVISION: 7.0        LAST MODIFIED: 07/17/92     BY: pma *F779*      */
/* REVISION: 7.0        LAST MODIFIED: 08/17/92     BY: pma *F857*      */
/* REVISION: 7.3        LAST MODIFIED: 10/30/92     BY: pma *G260*      */
/* REVISION: 7.3        LAST MODIFIED: 12/30/92     BY: pma *G488*      */
/* REVISION: 7.3        LAST MODIFIED: 02/16/93     BY: emb *G701*      */
/* REVISION: 7.3        LAST MODIFIED: 02/19/93     BY: pma *G681*      */
/* REVISION: 7.3        LAST MODIFIED: 09/23/93     BY: ram *GF82*      */
/* REVISION: 7.3        LAST MODIFIED: 12/29/93     BY: ais *FL07*      */
/* REVISION: 7.3        LAST MODIFIED: 03/24/94     BY: pma *GJ17*      */
/* REVISION: 7.5        LAST MODIFIED: 10/20/94     BY: mwd *J034*      */
/* REVISION: 7.3        LAST MODIFIED: 03/27/95     BY: qzl *F0P3*      */
/* REVISION: 8.5        LAST MODIFIED: 08/21/95     BY: wep *J054*      */
/* REVISION: 7.3        LAST MODIFIED: 10/02/95     BY: qzl *G0Y7*      */
/* REVISION: 7.3        LAST MODIFIED: 11/20/95     BY: bcm *G1DD*      */
/* REVISION: 7.3        LAST MODIFIED: 12/14/95     BY: qzl *G1BD*      */
/* REVISION: 7.3        LAST MODIFIED: 01/02/96     BY: bcm *G1HV*      */
/* REVISION: 7.3        LAST MODIFIED: 05/20/96     BY: rvw *G1W4*      */
/* REVISION: 8.6        LAST MODIFIED: 06/11/96     BY: ejh *K001*      */
/* REVISION: 8.5        LAST MODIFIED: 08/26/96 BY: *G1VX* Julie Milligan    */
/* REVISION: 8.6        LAST MODIFIED: 12/30/96 BY: *K03X* Julie Milligan    */
/* REVISION: 8.6        LAST MODIFIED: 09/30/97 BY: *G2PQ* Manmohan Pardesi  */

/* REVISION: 8.6E     LAST MODIFIED: 02/23/98   BY: *L007* A. Rahane         */
/* REVISION: 8.6E     LAST MODIFIED: 07/23/98   BY: *J2TK* Thomas Fernandes  */
/* REVISION: 8.6E     LAST MODIFIED: 07/23/98   BY: *H1H4* Dana Tunstall     */
/* REVISION: 8.6E     LAST MODIFIED: 08/12/98   BY: *J2VJ* Sandesh Mahagaokar*/
/* REVISION: 8.6E     LAST MODIFIED: 10/18/99   BY: *K21S* Mugdha Tambe      */
/* REVISION: 9.0      LAST MODIFIED: 02/14/00   BY: *J3NY* Jyoti Thatte      */
/* REVISION: 9.0      LAST MODIFIED: 02/24/00   BY: *J3P8* Jyoti Thatte      */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane  */
/* REVISION: 9.1      LAST MODIFIED: 08/12/00   BY: *N0KP* myb               */
/* Old ECO marker removed, but no ECO header exists *F0PN*                   */
/* Revision: 1.11.1.8     BY: Hualin Zhong      DATE: 05/08/01 ECO: *N0YC*   */
/* Revision: 1.11.1.9     BY: Rajesh Thomas     DATE: 06/27/01 ECO: *M1CD*   */
/* Revision: 1.11.1.10    BY: Katie Hilbert     DATE: 02/25/02 ECO: *N194*   */
/* Revision: 1.11.1.12    BY: Samir Bavkar      DATE: 04/12/02 ECO: *P000*   */
/* $Revision: 1.11.1.13 $           BY: Vivek Gogte       DATE: 08/06/02 ECO: *N1QQ*   */
/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/*F003*******************************************************************/
/*    MODIFIED LOGIC BELOW AS FOLLOWS:                                  */
/*    CHANGED ALL COST REFERENCES TO SCT_DET FILE                       */
/*    MODIFY DEFINE VARIABLES LIST AS REQUIRED                          */
/*    MODIFIED INPUT/SELECTION SCREEN TO SELECT BY SITE AND COST SET    */
/*    CHANGED LBR_TL & BDN_TL CALCULATIONS TO USE WC_SETUP_RTE          */
/*F003*******************************************************************/

/* LINKED-SITE COSTING CHANGES:                                              */
/*  -IF THE COST SET SPECIFIED IN THE SELECTION IS OF TYPE GL AND STANDARD   */
/*   METHOD THEN THE ROUTING COST ROLLUP WILL NOT UPDATE THIS-LEVEL COST IF  */
/*   THE ITEM AT THE ROLLUP SITE IS LINKED TO ANOTHER SITE FOR COST.         */
/*  -THE LEAD TIMES AND YIELD WOULE BE CALCULATED FOR ALL SELECTED ITEMS     */
/*   WHETHER THEY ARE LINKED OR NOT.                                         */


/* DISPLAY TITLE */
{mfdtitle.i "2+ "}

{gldydef.i new}
{gldynrm.i new}

define variable eff_date as date label "As of Date" initial today.
define variable part1 like pt_part.
define variable part2 like pt_part.
define variable part_type like pt_part_type.
define variable labor_flag like mfc_logical initial yes
   label "Roll-up Labor Time".
define variable setup_flag like mfc_logical initial yes
   label "Roll-up Setup Time".
define variable yield_flag like mfc_logical initial yes
   label "Roll-up Item Yield".
define variable cum_flag like mfc_logical initial yes
   label "Roll-up Lead Time".
define variable lbr_flag like mfc_logical initial yes
   label "Roll-up Labor Cost".
define variable bdn_flag like mfc_logical initial yes
   label "Roll-up Burden Cost".
define variable sub_flag like mfc_logical initial yes
   label "Roll-up Subcontract Cost".
define variable update_all like mfc_logical initial true
   label "Update Items without Routings".
define variable inc_yield like mfc_logical initial true
   label "Include Yield in Cost".
define variable update-op like mfc_logical initial true
   label "Update Operation Cost".
define variable existing_items like mfc_logical initial yes
   label "Update Items At This Site Only" no-undo.

define variable oldcst      like sct_cst_tot no-undo.
define variable run_time    as decimal no-undo.
define variable setup_time  as decimal no-undo.
define variable part_yield  as decimal no-undo.
define variable lbr_tl      as decimal no-undo.
define variable bdn_tl      as decimal no-undo.
define variable sub_tl      as decimal no-undo.
define variable ord_qty     like pt_ord_qty no-undo.
define variable update_flag like mfc_logical no-undo.
define variable days        as integer no-undo.
define variable i           as integer no-undo.
define variable site        like si_site no-undo.
define variable csset       like sct_sim no-undo.
define variable method      as character format "x(15)" no-undo.
define variable routing     like ro_routing no-undo.
define variable phantom     like pt_phantom no-undo.
define variable cum_yield   as decimal no-undo.
define variable bom_code    like pt_bom_code no-undo.
define variable cur_set     like icc_cur_set no-undo.
define variable mtl_this_op as decimal no-undo.
define variable lbr_this_op as decimal no-undo.
define variable bdn_this_op as decimal no-undo.
define variable sub_this_op as decimal no-undo.
define variable ovh_this_op as decimal no-undo.
define variable mtl_prev_op as decimal no-undo.
define variable lbr_prev_op as decimal no-undo.
define variable bdn_prev_op as decimal no-undo.
define variable sub_prev_op as decimal no-undo.
define variable ovh_prev_op as decimal no-undo.
define variable mtl_in      as decimal no-undo.
define variable lbr_in      as decimal no-undo.
define variable bdn_in      as decimal no-undo.
define variable sub_in      as decimal no-undo.
define variable ovh_in      as decimal no-undo.
define variable frozen      like mfc_logical.
define variable flags       as character no-undo.
define variable rollup_complete as character format "x(64)" no-undo.
define variable l_gl_std like mfc_logical no-undo.
define variable l_linked like mfc_logical no-undo.

{gpspt01a.i "new shared"}


/*GUI preprocessor Frame A define */
&SCOPED-DEFINE PP_FRAME_NAME A

FORM /*GUI*/ 
   

&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN
 RECT-FRAME       AT ROW 1 COLUMN 1.25
 RECT-FRAME-LABEL AT ROW 1 COLUMN 3 NO-LABEL VIEW-AS TEXT SIZE-PIXELS 1 BY 1
 SKIP(.1)  /*GUI*/
&ENDIF /*GUI*/
site           colon 18 si_desc no-label
   csset          colon 18 cs_desc no-label
   method         no-label skip(1)
   part1          colon 18
   part2          label {t001.i} colon 49 skip (1)
   part_type      colon 32
   eff_date       colon 32
   labor_flag     colon 32
   setup_flag     colon 32
   cum_flag       colon 32
   yield_flag     colon 32
   inc_yield      colon 64
   skip(1)
   lbr_flag       colon 32
   bdn_flag       colon 32
   sub_flag       colon 32
   update_all     colon 32
   existing_items colon 32


&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN
 SKIP(.4)  /*GUI*/
&ENDIF /*GUI*/
with frame a side-labels width 80 attr-space
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN
 NO-BOX THREE-D /*GUI*/
&ENDIF /*GUI*/
.


&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN
 DEFINE VARIABLE F-a-title AS CHARACTER INITIAL "".
 RECT-FRAME-LABEL:SCREEN-VALUE in frame a = F-a-title.
 RECT-FRAME-LABEL:HIDDEN in frame a = yes.
 RECT-FRAME:HEIGHT-PIXELS in frame a =
  FRAME a:HEIGHT-PIXELS - RECT-FRAME:Y in frame a - 2.
 RECT-FRAME:WIDTH-CHARS IN FRAME a = FRAME a:WIDTH-CHARS - .5.  /*GUI*/
&ENDIF /*GUI*/

/*GUI preprocessor Frame A undefine */
&UNDEFINE PP_FRAME_NAME



/* SET EXTERNAL LABELS */
setFrameLabels(frame a:handle).

site = global_site.

for first icc_ctrl
   fields(icc_cogs icc_gl_sum icc_gl_tran icc_mirror
          icc_jrnl icc_cur_set icc_gl_set)
no-lock: end.
if not available icc_ctrl then do:

   /* PLEASE CREATE INVENTORY CONTROL FILE */
   {pxmsg.i &MSGNUM=533
            &ERRORLEVEL=4
            &MSGARG1=getTermLabel(""INVENTORY"",12)}

   repeat:
      if batchrun = no then pause.
      leave.
   end.
   undo, leave.
end.

assign
   cur_set = icc_cur_set.
   csset = cur_set.

find si_mstr no-lock where si_site = site no-error.
if available si_mstr then do:
   display site si_desc with frame a.
   if si_cur_set > "" then csset = si_cur_set.
end.

display csset with frame a.
find cs_mstr where cs_set = csset no-lock no-error.
method = "".
if available cs_mstr then do:
   method = "[ " + cs_method + " / " + cs_type + " ]".
   display cs_desc method with frame a.
end.
else display "" @ cs_desc method with frame a.

display
   site
   part1 part2
   part_type
   eff_date
   labor_flag
   setup_flag
   cum_flag
   yield_flag
   inc_yield
   lbr_flag
   bdn_flag
   sub_flag
   update_all
   existing_items
with frame a.

mainloop:
repeat:
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


   if part2 = hi_char then part2 = "".

   if not batchrun then do:
      seta1:
      do transaction on error undo, retry:
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

         set site with frame a
         editing:
            /* FIND NEXT/PREVIOUS RECORD */
            {mfnp.i si_mstr site si_site site si_site si_site}

            if recno <> ? then do:
               site = si_site.
               display
                  site
                  si_desc
               with frame a.

               csset = cur_set.
               if si_cur_set > "" then csset = si_cur_set.
               display csset with frame a.
               find cs_mstr where cs_set = csset no-lock no-error.
               method = "".
               if available cs_mstr then do:
                  method = "[ " + cs_method + " / " + cs_type + " ]".
                  display cs_desc method with frame a.
               end.
               else display "" @ cs_desc method with frame a.

               recno = ?.
            end.
         end. /* EDITING */

         find si_mstr where si_site = site no-lock no-error.
         if not available si_mstr then do:
            {pxmsg.i &MSGNUM=708 &ERRORLEVEL=3} /* Site does not exist*/
            next-prompt site with frame a.
            undo, retry.
         end.

         display si_desc with frame a.

         if si_db <> global_db then do:
            /* Site is not assigned to this database */
            {pxmsg.i &MSGNUM=5421 &ERRORLEVEL=3}
            next-prompt site with frame a.
            undo, retry.
         end.

         {gprun.i ""gpsiver.p""
            "(input site, input recid(si_mstr), output return_int)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

         if return_int = 0 then do:
            /* USER DOES NOT HAVE ACCESS TO THIS SITE */
            {pxmsg.i &MSGNUM=725 &ERRORLEVEL=3}
            next-prompt site with frame a.
            undo mainloop, retry mainloop.
         end.

         csset = cur_set.
         if si_cur_set > "" then csset = si_cur_set.
         display csset with frame a.
         find cs_mstr where cs_set = csset no-lock no-error.
         method = "".
         if available cs_mstr then do:
            method = "[ " + cs_method + " / " + cs_type + " ]".
            display cs_desc method with frame a.
         end.
         else display "" @ cs_desc method with frame a.
      end.
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


      seta2:
      do transaction on error undo, retry:
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

         set csset with frame a
         editing:
            /* FIND NEXT/PREVIOUS RECORD */
            {mfnp.i cs_mstr csset cs_set csset cs_set cs_set}

            if recno <> ? then do:
               csset = cs_set.
               display csset with frame a.
               find cs_mstr where cs_set = csset no-lock no-error.
               method = "".
               if available cs_mstr then do:
                  method = "[ " + cs_method + " / " + cs_type + " ]".
                  display cs_desc method with frame a.
               end.
               recno = ?.
            end.
         end.

         if csset = "" then do:
            {pxmsg.i &MSGNUM=40 &ERRORLEVEL=3}  /*Blank not allowed*/
            next-prompt csset.
            undo, retry.
         end.

         find cs_mstr where cs_set = csset no-lock no-error.
         if not available cs_mstr then do:
            {pxmsg.i &MSGNUM=5407 &ERRORLEVEL=3} /*Cost set does not exist*/
            next-prompt csset.
            undo, retry.
         end.

         method = "[ " + cs_method + " / " + cs_type + " ]".
         display cs_desc method with frame a.
         if cs_method = "AVG" and cs_type = "CURR" then do:
            /*Roll-up will invalidate avg cost*/
            {pxmsg.i &MSGNUM=5419 &ERRORLEVEL=2}
         end.
         else if cs_method = "AVG" and cs_type = "GL" then do:
            /*Roll-up will invalidate avg cost*/
            {pxmsg.i &MSGNUM=5419 &ERRORLEVEL=4}
            next-prompt csset.
            undo, retry.
         end.
      end.
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


      update
         part1 part2
         part_type
         eff_date
         labor_flag
         setup_flag
         cum_flag
         yield_flag
         inc_yield
         lbr_flag
         bdn_flag
         sub_flag
         update_all
         existing_items
      with frame a.

      /* ADDED BATCH RUN CAPABILITY */

   end. /* IF NOT BATCHRUN */

   else do:
      update
         site
         csset
         part1 part2
         part_type
         eff_date
         labor_flag
         setup_flag
         cum_flag
         yield_flag
         inc_yield
         lbr_flag
         bdn_flag
         sub_flag
         update_all
         existing_items
      with frame a.
      for first si_mstr
         fields (si_cur_set si_db si_desc si_site)
         where si_site = site no-lock:
      end. /* FOR FIRST SI_MSTR */
   end.
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/
 /* ELSE DO */

   /*CREATE FLAGS TO REPRESENT WHICH COST CATEGORIES CAN BE */
   /*UPDATED: 2 - LABOR, 3 - BURDEN, 5 - SUBCONTRACT        */

   flags = "".
   if lbr_flag then flags = "2,".
   if bdn_flag then flags = flags + "3,".
   if sub_flag then flags = flags + "5,".

   if part2 = "" then part2 = hi_char.

   /*Begin routing roll-up ? */

   if not labor_flag and
      not setup_flag and
      not cum_flag   and
      not yield_flag and
      not inc_yield  and
      not lbr_flag   and
      not bdn_flag   and
      not sub_flag
   then next.

   bcdparm = "".

   {gprun.i ""gpquote.p"" "(input-output bcdparm,
               16,
               site,
               csset,
               part1,
               part2,
               part_type,
               string(eff_date),
               string(labor_flag),
               string(setup_flag),
               string(cum_flag),
               string(yield_flag),
               string(inc_yield),
               string(lbr_flag),
               string(bdn_flag),
               string(sub_flag),
               string(update_all),
               string(existing_items),
               null_char, null_char, null_char,null_char
               )"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


   if eff_date = ?
   then do:
      eff_date =  today.
      display eff_date with frame a.
   end. /* IF eff_date ... */

   /* OUTPUT DESTINATION SELECTION */
   {gpselout.i &printType = "printer"
               &printWidth = 132
               &pagedFlag = " "
               &stream = " "
               &appendToFile = " "
               &streamedOutputToTerminal = " "
               &withBatchOption = "yes"
               &displayStatementType = 1
               &withCancelMessage = "yes"
               &pageBottomMargin = 6
               &withEmail = "yes"
               &withWinprint = "yes"
               &defineVariables = "yes"}
/*GUI*/ RECT-FRAME:HEIGHT-PIXELS in frame a = FRAME a:HEIGHT-PIXELS - RECT-FRAME:Y in frame a - 2.

   {mfphead.i}

   /* RECOGNIZE SITE LINKS FOR GL COST WHEN A GL STANDARD COST SET IS */
   /* SPECIFIED */

   /* CHECK IF THE COST SET ENTERED IS OF TYPE GL AND STANDARD METHOD */
   l_gl_std = false.

   if can-find (first cs_mstr where cs_set = csset
                and cs_type = "GL" and cs_method = "STD") then
      l_gl_std = true.

   itemloop:

   for each pt_mstr
      fields (pt_bom_code  pt_mfg_lead pt_mrp pt_ord_qty pt_part
              pt_part_type pt_pm_code  pt_routing  pt_run
              pt_setup pt_yield_pct)
      no-lock
      where pt_part >= part1 and pt_part <= part2
       and (part_type = "" or part_type = pt_part_type):

      /* IF THE EXISTING_ITEMS FLAG IS SET TO YES AND IF THERE ARE   */
      /* NO INVENTORY MASTER RECORDS FOR THAT ITEM AT THAT SPECIFIED */
      /* SITE THEN THE COST WON'T BE ROLLED UP FOR THAT ITEM TO      */
      /* AVOID CREATION OF UNWANTED RECORDS                          */

      if existing_items then do:
         if not can-find(in_mstr where in_part = pt_part
                                 and   in_site = site)
         then next itemloop.
      end. /* IF EXISTING_ITEMS THEN */

      /* CHECK IF THIS ITEM IS LINKED TO ANOTHER SITE FOR COST  */
      l_linked = false.

      if l_gl_std then
         if can-find( first in_mstr
                      where in_part = pt_part
                        and in_site = site
                        and in_site <> in_gl_cost_site) then
            l_linked = true.

      frozen = no.
      find sct_det no-lock
         where sct_sim  = csset
           and sct_part = pt_part
           and sct_site = site no-error.

      if available sct_det and sct_rollup then frozen = yes.

      ord_qty = 1.
      if pt_ord_qty <> 0 then ord_qty = pt_ord_qty.

      assign
         run_time = 0
         setup_time = 0
         part_yield = 100
         cum_yield = 100
         lbr_tl = 0
         bdn_tl = 0
         sub_tl = 0
         update_flag = no
         mtl_this_op = 0
         lbr_this_op = 0
         bdn_this_op = 0
         sub_this_op = 0
         ovh_this_op = 0
         mtl_prev_op = 0
         lbr_prev_op = 0
         bdn_prev_op = 0
         sub_prev_op = 0
         ovh_prev_op = 0
         mtl_in = 0
         lbr_in = 0
         bdn_in = 0
         sub_in = 0
         ovh_in = 0.

      if update_all then update_flag = true.

      assign
         routing = pt_part
         bom_code = pt_part.

      find ptp_det no-lock where ptp_part = pt_part
         and ptp_site = site no-error.
      if available ptp_det then do:
         if ptp_routing <> "" then routing = ptp_routing.
         if ptp_bom_code <> "" then bom_code = ptp_bom_code.
         if ptp_ord_qty <> 0 then ord_qty = ptp_ord_qty.
      end.
      else do:
         if pt_routing > "" then routing = pt_routing.
         if pt_bom_code > "" then bom_code = pt_bom_code.
      end.

      /* EXCLUDE CO-PRODUCT ITEMS FROM COST ROLL-UP */

      for first ps_mstr
         fields( ps_comp  ps_end  ps_joint_type
                 ps_op ps_par ps_ps_code
                 ps_qty_per ps_start ps_scrp_pct )
            where ps_par   =  pt_mstr.pt_part
            and   ps_comp  =  bom_code
            and   ps_joint_type = "1"
            and   (eff_date = ?
            or    (eff_date <> ?
            and   (ps_start = ? or ps_start <= eff_date)
            and   (ps_end = ? or ps_end >= eff_date )))
            no-lock:
      end. /* FOR FIRST PS_MSTR */
      if available ps_mstr then next itemloop .

      if update-op
         and not frozen
      then do:
         for each iro_det exclusive-lock
             where iro_part = bom_code
               and iro_site = site
               and iro_cost_set = csset
               and iro_routing = routing:
            delete iro_det.
         end.
      end.

      for each ro_det where ro_routing = routing
            and (eff_date = ? or ro_start = ? or ro_start <= eff_date)
            and (eff_date = ? or ro_end = ? or eff_date <= ro_end)
            no-lock break by ro_routing by ro_op:
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


         assign
            update_flag = yes
            run_time = run_time + ro_run
            setup_time = setup_time + ro_setup.

         /* TO INCLUDE YIELD % ONLY IF INCLUDE YIELD IN COST = YES */

         if inc_yield then
            sub_tl = sub_tl + (ro_sub_cost * part_yield * .01
                     * ro_yield_pct * .01).
         else
            sub_tl = sub_tl + ro_sub_cost.

         /* Note:                         */
         /* Yield handled differently for */
         /* subcontract.  You normally    */
         /* only pay for GOOD sub parts,  */
         /* while you expend lbr & bdn to */
         /* make both GOOD and BAD parts  */

         if update-op
            and first-of (ro_routing)
            and ro_op <> 0
         then do:
            for each ps_mstr no-lock where ps_par = bom_code
                  and ps_op = 0
                  and (eff_date = ? or ps_start = ? or ps_start <= eff_date)
                  and (eff_date = ? or ps_end = ? or eff_date <= ps_end)
                  and can-find (first pt_mstr where pt_part = ps_comp)
                  and (ps_ps_code = "" or ps_ps_code = "X"):
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


              {gpsct01.i &part=ps_comp &set=csset &site=site}

               /* CHECK FOR GLOBAL PHANTOM; IF SO, ONLY ROLL-UP */
               /* LOWER LEVEL COSTS TO THE PARENT               */
               {gprun.i ""gpckphan.p"" "(input ps_comp,
                                         input site,
                                         output phantom)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

               if not phantom then
                  mtl_prev_op = mtl_prev_op + sct_cst_tot
                              * ps_qty_per * (100 / (100 - ps_scrp_pct)).
               else
                  mtl_prev_op = mtl_prev_op
                              + (sct_bdn_ll + sct_lbr_ll + sct_mtl_ll +
                                 sct_ovh_ll + sct_sub_ll)
                              * ps_qty_per * (100 / (100 - ps_scrp_pct)).
            end.
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

            mtl_in = mtl_prev_op.
         end.

         find wc_mstr no-lock where wc_wkctr = ro_wkctr
            and wc_mch = ro_mch no-error.

         if available wc_mstr then do:

            lbr_tl = lbr_tl
                   + (((ro_setup / ord_qty) * wc_setup_rte)
                   + (ro_run * wc_lbr_rate))
                   * part_yield * .01.

            /*! CALCULATE TOTAL OPERATION BURDEN USING THE STANDARD  *
            *  EQUATION.                                            */
            {gprun.i ""rwbdncal.p""
               "( input 'TOTAL'           /* BURDEN COST TYPE */,
                  input yes              /* ACCUMULATE TOTAL? */,
                  input (wc_bdn_pc * 0.01)  /* BURDEN PERCENT */,
                  input wc_bdn_rate            /* BURDEN RATE */,
                  input wc_lbr_rate             /* LABOR RATE */,
                  input wc_mch_bdn     /* MACHINE BURDEN RATE */,
                  input ro_mch_op            /* MACHINES USED */,
                  input ord_qty             /* ORDER QUANTITY */,
                  input ro_run               /* UNIT RUN TIME */,
                  input wc_setup_rte            /* SETUP RATE */,
                  input ro_setup                /* SETUP TIME */,
                  input (part_yield * 0.01)  /* YIELD PERCENT */,
                  input-output bdn_tl          /* BURDEN COST */)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


         end. /* if available wc_mstr */

         if update-op
         then do:
            /* TO INCLUDE YIELD % ONLY IF INCLUDE YIELD IN COST = YES */

            if inc_yield then
               sub_this_op = (ro_sub_cost * part_yield * .01
                           * ro_yield_pct * .01).
            else
               sub_this_op =  ro_sub_cost.

            if available wc_mstr then
               lbr_this_op = (((ro_setup / ord_qty) * wc_setup_rte)
                       + (ro_run * wc_lbr_rate)) * part_yield * .01.
            else
               lbr_this_op = 0.
            if available wc_mstr then
               bdn_this_op = (((ro_setup / ord_qty)
                  * (wc_setup_rte * wc_bdn_pc * .01
                  + wc_bdn_rate + wc_mch_bdn * ro_mch_op))
                  + (ro_run * (wc_lbr_rate * wc_bdn_pc * .01
                  + wc_bdn_rate + wc_mch_bdn)))
                  * part_yield * .01.
            else
               bdn_this_op = 0.

            for each ps_mstr no-lock
               where ps_par = bom_code
                 and ps_op = ro_op
                 and (eff_date = ? or ps_start = ?
                 or ps_start <= eff_date)
                 and (eff_date = ? or ps_end = ? or eff_date <= ps_end)
                 and can-find (first pt_mstr where pt_part = ps_comp)
                 and (ps_ps_code = "" or ps_ps_code = "X"):
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


               {gpsct01.i &part=ps_comp &set=csset &site=site}

               /* CHECK FOR GLOBAL PHANTOM; IF SO, ONLY ROLL-UP */
               /* LOWER LEVEL COSTS TO THE PARENT               */
               {gprun.i ""gpckphan.p"" "(input ps_comp,
                                         input site,
                                         output phantom)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

               if not phantom then
                  mtl_this_op = mtl_this_op + sct_cst_tot
                     * ps_qty_per * (100 / (100 - ps_scrp_pct))
                     * part_yield * .01.
               else
                  mtl_this_op = mtl_this_op +
                     (sct_bdn_ll + sct_lbr_ll + sct_mtl_ll +
                      sct_ovh_ll + sct_sub_ll) * ps_qty_per
                     * (100 / (100 - ps_scrp_pct))
                     * part_yield * .01.
            end.
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


            assign
               mtl_in = mtl_in + mtl_this_op
               lbr_in = lbr_in + lbr_this_op
               bdn_in = bdn_in + bdn_this_op
               sub_in = sub_in + sub_this_op
               ovh_in = bdn_in + ovh_this_op.

         end.

         cum_yield = cum_yield * .01 * ro_yield_pct.
         if inc_yield then part_yield = cum_yield.

         /***********************************************************/
         /* THE FOLLOWING CODE WOULD BE INSERTED HERE IN ORDER TO   */
         /* CALCULATE THE CUMULATIVE COMPLETED COST AT THE END OF   */
         /* EACH OPERATION:                                         */
         /*  operation subcontract = sub_tl   / (part_yield * .01)  */
         /*  operation labor       = lbr_tl   / (part_yield * .01)  */
         /*  operation burden      = bdn_tl   / (part_yield * .01)  */
         /*                        + cum matl / (part_yield * .01)  */
         /*                          =============================  */

         /***********************************************************/

         if update-op
            and not frozen
         then do:
            find iro_det exclusive-lock
               where iro_part = bom_code
                 and iro_site = site
                 and iro_cost_set = csset
                 and iro_routing = ro_routing
                 and iro_op = ro_op
               no-error.
            if not available iro_det then do:
               create iro_det.
               assign
                  iro_part = bom_code
                  iro_site = site
                  iro_cost_set = csset
                  iro_routing = ro_routing
                  iro_op = ro_op.
            end.

            assign
               iro_date = eff_date
               iro_mtl_tl = mtl_in / (part_yield * .01)
                          - mtl_prev_op
               iro_lbr_tl = lbr_in / (part_yield * .01)
                          - lbr_prev_op
               iro_bdn_tl = bdn_in / (part_yield * .01)
                          - bdn_prev_op
               iro_sub_tl = sub_in / (part_yield * .01)
                          - sub_prev_op
               iro_mtl_ll = mtl_prev_op
               iro_lbr_ll = lbr_prev_op
               iro_bdn_ll = bdn_prev_op
               iro_sub_ll = sub_prev_op
               iro_cost_tot = iro_mtl_tl + iro_mtl_ll
                            + iro_lbr_tl + iro_lbr_ll
                            + iro_bdn_tl + iro_bdn_ll
                            + iro_sub_tl + iro_sub_ll
                            + iro_ovh_tl + iro_ovh_ll.

            assign
               mtl_prev_op = iro_mtl_tl + iro_mtl_ll
               lbr_prev_op = iro_lbr_tl + iro_lbr_ll
               bdn_prev_op = iro_bdn_tl + iro_bdn_ll
               sub_prev_op = iro_sub_tl + iro_sub_ll
               ovh_prev_op = iro_ovh_tl + iro_ovh_ll
               mtl_this_op = 0
               lbr_this_op = 0
               bdn_this_op = 0
               sub_this_op = 0
               ovh_this_op = 0.
         end.
      end.
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/
 /*for each ro_det*/

      if inc_yield then
         assign
            sub_tl = sub_tl / (part_yield * .01)
            lbr_tl = lbr_tl / (part_yield * .01)
            bdn_tl = bdn_tl / (part_yield * .01).

      /*IN THE FOLLOWING STATEMENTS 'cum_yield' WAS PREVIOUSLY 'part_yield'*/

      /* Calculation of MFG LT is now done entirely within
      a set of subroutines which simulate scheduling of a
      work order for the standard order quantity and then
      extract the MFG LT from those results (difference
      between release and due date for the temporary order). */
      if cum_flag then do:
         {gprun.i ""rwrorua.p""
            "(pt_part,site,routing,ord_qty,eff_date,output days)"}
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/

      end.

      if update_flag then run update-costs.

   end. /*for each pt_mstr*/

   {pxmsg.i &MSGNUM = 4646
            &ERRORLEVEL = 1
            &MSGBUFFER = rollup_complete}

   display rollup_complete with no-label
&IF ("{&PP_GUI_CONVERT_MODE}" = "REPORT") &THEN
 STREAM-IO /*GUI*/ 
&ENDIF /*GUI*/
.

   {mfrtrail.i}

end. /*repeat*/

/* * * INTERNAL PROCEDURES * * * * * * * * * * * * * * * * * * * * * * * * */

PROCEDURE update-costs:

   define buffer ptmstr for pt_mstr.
   define variable rndmthd like gl_rnd_mthd no-undo.
   for first gl_ctrl fields(gl_rnd_mthd) no-lock: end.
   if available gl_ctrl then rndmthd = gl_rnd_mthd.

   do for ptmstr:

      find ptmstr exclusive-lock where
         recid(ptmstr) = recid(pt_mstr).
      find ptp_det exclusive-lock where ptp_part = pt_part
         and ptp_site = site no-error.

      if available ptp_det then do:
         if (yield_flag and ptp_yld_pct <> cum_yield)
         or (cum_flag and ptp_mfg_lead <> days
             and ptp_pm_code <> "P" and ptp_pm_code <> "D")
         then do:
            find in_mstr exclusive-lock
               where in_part = pt_part
                 and in_site = site
                 and in_mrp  = no no-error.
            if available in_mstr then in_mrp = yes.
            pt_mrp = yes.
         end.

         if labor_flag then ptp_run = run_time.
         if setup_flag then ptp_setup = setup_time.
         if cum_flag then ptp_mfg_lead = days.
         if yield_flag then ptp_yld_pct = cum_yield.
      end.
      else do:

         if (yield_flag and pt_yield_pct <> cum_yield)
            or (cum_flag and pt_mfg_lead <> days and pt_pm_code <> "P")
         then do:
            find in_mstr exclusive-lock
               where in_part = pt_part
                 and in_site = site
                 and in_mrp  = no no-error.
            if available in_mstr then in_mrp = yes.
            pt_mrp = yes.
         end.

         if labor_flag then pt_run = run_time.
         if setup_flag then pt_setup = setup_time.
         if cum_flag then pt_mfg_lead = days.
         if yield_flag then pt_yield_pct = cum_yield.
      end.

      /* IF A GL STANDARD COST SET IS SPECIFIED UPDATE THIS-LEVEL COSTS */
      /* ONLY IF THIS ITEM IS NOT LINKED.                               */
      if not l_linked then do:
         {gpsct01.i &part=pt_part &set=csset &site=site}
         oldcst = sct_cst_tot.

         if (available ptp_det
            and (ptp_pm_code = "P" or ptp_pm_code = "D"))
            or (not available ptp_det
            and (pt_pm_code = "P" or pt_pm_code = "D"))
         then
            assign
               lbr_tl = 0
               bdn_tl = 0
               sub_tl = 0.

         if ((available ptp_det
            and (ptp_pm_code = "P" or ptp_pm_code = "D"))
            or (not available ptp_det
            and (pt_pm_code = "P" or pt_pm_code = "D"))
            or lbr_flag or bdn_flag or sub_flag)
            and not frozen
         then do:

            {gpspt01c.i &sct=recid(sct_det)}
            if lbr_flag then do:
               find spt_det where recid(spt_det) = spt_reclbr.
               spt_cst_tl = lbr_tl.
            end.
            if bdn_flag then do:
               find spt_det where recid(spt_det) = spt_recbdn.
               spt_cst_tl = bdn_tl.
            end.
            if sub_flag then do:
               find spt_det where recid(spt_det) = spt_recsub.
               spt_cst_tl = sub_tl.
            end.

            assign
               sct_mtl_tl = 0
               sct_lbr_tl = 0
               sct_bdn_tl = 0
               sct_ovh_tl = 0
               sct_sub_tl = 0.

            for each spt_det
                  where spt_site = sct_site
                  and spt_sim = sct_sim
                  and spt_part = sct_part:
               if (truncate(spt_pct_ll,2) - truncate(spt_pct_ll,0)) > 0
                  and (truncate(spt_pct_ll,0) <> 1)
                  and (truncate(spt_pct_ll,0) <> 4)
                  and can-do(flags,string(truncate(spt_pct_ll,0),"9"))
               then
                  spt_cst_tl = 0.
               else
               if truncate(spt_pct_ll,0) = 1 then
                  sct_mtl_tl = sct_mtl_tl + spt_cst_tl.
               else
               if truncate(spt_pct_ll,0) = 2 then
                  sct_lbr_tl = sct_lbr_tl + spt_cst_tl.
               else
               if truncate(spt_pct_ll,0) = 3 then
                  sct_bdn_tl = sct_bdn_tl + spt_cst_tl.
               else
               if truncate(spt_pct_ll,0) = 4 then
                  sct_ovh_tl = sct_ovh_tl + spt_cst_tl.
               else
               if truncate(spt_pct_ll,0) = 5 then
                  sct_sub_tl = sct_sub_tl + spt_cst_tl.
            end.

            sct_cst_tot = sct_mtl_tl + sct_mtl_ll + sct_lbr_tl + sct_lbr_ll
                        + sct_bdn_tl + sct_bdn_ll + sct_ovh_tl + sct_ovh_ll
                        + sct_sub_tl + sct_sub_ll.

            if sct_cst_tot <> oldcst then do:
               sct_cst_date = today.

               if not available icc_ctrl
                  then find first icc_ctrl no-lock.

               find in_mstr no-lock where in_mstr.in_part = pt_part
                  and in_mstr.in_site = site no-error.
               if available in_mstr and (in_mstr.in_gl_set = sct_sim
                  or (sct_sim = icc_gl_set and in_mstr.in_gl_set = ""))
               then do:
                  trmsg = 5250.

                  /*DE-COUPLE PROGRAMS FOR OBJECTS*/
                  {gprun.i 'iccstinv.p'
                     "(input oldcst,
                       buffer sct_det,
                       input 'CCHG',
                       input in_mstr.in_site,
                       input icc_cogs,
                       input icc_gl_sum,
                       input icc_gl_tran,
                       input icc_mirror,
                       input icc_jrnl,
                       input rndmthd,
                       input pt_part,
                       input pt_prod_line,
                       input pt_site,
                       input pt_um)"
                     }
&IF ("{&PP_GUI_CONVERT_MODE}" <> "REPORT") OR
(("{&PP_GUI_CONVERT_MODE}" = "REPORT") AND
("{&PP_FRAME_NAME}" = "A")) &THEN

/*GUI*/ if global-beam-me-up then undo, leave.
&ENDIF /*GUI*/


               end. /*if avail in_mstr*/
            end. /*if sct_cst_tot <> oldcst*/
         end. /*if pt_pm_code*/
      end. /* IF NOT LINKED  */
   end. /* do for ptmstr */
end. /* PROCEDURE update-costs */
